package com.lan.utils;

import java.util.Random;

public class RandomUtils {

    private static final String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    private static Random random = new Random();

    /**
     * 根据长度随机生成一段随机字符串,随机内容为大小写字母加数字
     *
     * @param length
     * @return
     */
    public static String generateRandomStrings(int length) {
        Random rand = new Random();
        StringBuilder strBuilder = new StringBuilder();
        int strLen = str.length();
        for (int i = 0; i < length; i++) {
            char ch = str.charAt(rand.nextInt(strLen));
            strBuilder.append(ch);
        }
        return strBuilder.toString();
    }

    /**
     * 随机在某个范围的数字
     *
     * @param limit
     * @return
     */
    public static int random(int limit) {
        return random.nextInt(limit);
    }


    public static int random(int[] arr) {
        return arr[random.nextInt(arr.length)];
    }

    /**
     * 根据长度随机生成一段随机字符串,随机内容可自行设置
     *
     * @param strRange 随机内容选择范围
     * @param length   随机字符串长度
     * @return
     */
    public static String generateRandomStrings(String strRange, int length) {
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char ch = strRange.charAt(random.nextInt(strRange.length()));
            strBuilder.append(ch);
        }
        return strBuilder.toString();
    }

    /**
     * 随机卡密码
     *
     * @return
     */
    public static String randomCardKeyWord() {
        return generateRandomStrings("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10);
    }

    /**
     * 随机短信
     *
     * @param len
     * @return
     */
    public static String randomSMS(int len) {
        return generateRandomStrings("0123456789", len);
    }

    public static void main(String[] args) {
//		System.out.println(generateRandomStrings("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10));
        System.out.println(generateRandomStrings("0123456789", 10));
    }
}
